package com.yiren.manager.ctrl.system;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fasterxml.jackson.core.type.TypeReference;
import com.yiren.core.exception.MyException;
import com.yiren.core.exception.SwallowException;
import com.yiren.core.repository.utils.QueryResult;
import com.yiren.core.utils.FavoriteSet;
import com.yiren.core.utils.HTTPResult;
import com.yiren.entity.YrAccount;
import com.yiren.entity.YrAccountModelConfig;
import com.yiren.entity.YrStaffInfo;
import com.yiren.manager.ctrl.GenericController;
import com.yiren.manager.form.department.YrStaffInfoForm;
import com.yiren.manager.form.personnel.YrAccountForm;
import com.yiren.manager.form.system.YrAccountModelConfigForm;
import com.yiren.manager.service.iface.department.YrStaffInfoService;
import com.yiren.manager.service.iface.personnel.YrAccountService;
import com.yiren.manager.service.iface.system.YrAccountModelConfigService;
import com.yiren.core.utils.JsonMapperTool;
import com.yiren.core.utils.UUIDGenerator;
import com.yiren.manager.utils.ObjectUtils;

@RequestMapping("accountModelConfig")
@Controller
public class YrAccountModelConfigController extends GenericController<YrAccountModelConfigForm, YrAccountModelConfig>{

	@Autowired
	private YrAccountModelConfigService selfService;
	
	@Autowired
	private YrStaffInfoService staffInfoService;
	
	@Autowired
	private YrAccountService accountService ;
	
	@RequestMapping("allotRight")
	@ResponseBody
	public HTTPResult<YrAccountModelConfigForm> allotRight(String data) throws SwallowException, MyException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException{
		HTTPResult<YrAccountModelConfigForm> result = new HTTPResult<YrAccountModelConfigForm>();
		JsonMapperTool tool = new JsonMapperTool(data);
		ArrayList<YrAccountModelConfigForm> accountModelConfigForms = tool.mapper("accountModelConfigList",new TypeReference<ArrayList<YrAccountModelConfigForm>>(){});
		ArrayList<YrAccountModelConfigForm> deleteAccountModelConfigForms = tool.mapper("deleteAccountModelConfigList", new TypeReference<ArrayList<YrAccountModelConfigForm>>(){});
		
		FavoriteSet favoriteSet = new FavoriteSet();
		
		List<YrAccountModelConfig> accountModelConfigList = new ArrayList<YrAccountModelConfig>();
		List<YrAccountModelConfig> deleteAccountModelConfigList = new ArrayList<YrAccountModelConfig>();
		
		if(null!=accountModelConfigForms){
			for(YrAccountModelConfigForm form : accountModelConfigForms){
				if(form.getAccountModelConfigId()==null){
					form.setAccountModelConfigId(UUIDGenerator.getUUID());
					form.setCreateTime(new Date());
					accountModelConfigList.add(ObjectUtils.objectToObject(form, YrAccountModelConfig.class));
				}
			}
		}

		if(null!=deleteAccountModelConfigForms){
			for(YrAccountModelConfigForm form : deleteAccountModelConfigForms){
				if(form.getAccountModelConfigId()!=null){
					deleteAccountModelConfigList.add(ObjectUtils.objectToObject(form, YrAccountModelConfig.class));
				}
			}
		}
		
		favoriteSet.putFavorite("accountModelConfigList", accountModelConfigList);
		favoriteSet.putFavorite("deleteAccountModelConfigList", deleteAccountModelConfigList);
		
		selfService.allotRight(favoriteSet);
		
		return result;
	}
	
	@RequestMapping("findRights")
	@ResponseBody
	public HTTPResult<YrAccountModelConfigForm> findRights(String accountId) throws Exception{
		HTTPResult<YrAccountModelConfigForm> result = new HTTPResult<YrAccountModelConfigForm>();
		String sqlWhere = "account_id=?1";
		QueryResult<YrAccountModelConfig> qr = selfService.find(YrAccountModelConfig.class, sqlWhere,new Object[]{accountId});
		List<YrAccountModelConfigForm> formList = new ArrayList<YrAccountModelConfigForm>();
		for(YrAccountModelConfig entity : qr.getResultList()){
			formList.add(toForm(entity));
		}
		result.setData(formList);
		return result;
	}
	
	@RequestMapping("verfyRight")
	@ResponseBody
	public HTTPResult<YrAccountModelConfigForm> verfyRight(String accountId,String modelCode) throws MyException{
		HTTPResult<YrAccountModelConfigForm> result = new HTTPResult<YrAccountModelConfigForm>();
		String sqlWhere = "account_id=?1 AND model_code=?2";
		QueryResult<YrAccountModelConfig> qr = selfService.find(YrAccountModelConfig.class, sqlWhere, new Object[]{accountId,modelCode});
		if(qr.getTotalRecord()==0){
			throwExp("当前用户没有该页面权限");
		}
		return result;
	}
	
	
	@RequestMapping("findStaffAndRights")
	@ResponseBody
	public HTTPResult<YrStaffInfoForm> findStaffAndRights(String searchVal) throws Exception{
		HTTPResult<YrStaffInfoForm> result = new HTTPResult<YrStaffInfoForm>();
		String sqlWhere = "name=?1 OR card_id=?2";
		
		QueryResult<YrStaffInfo> qr = staffInfoService.find(YrStaffInfo.class, sqlWhere, new Object[]{searchVal,searchVal});
		if(qr.getTotalRecord()==0){
			throwExp("查不到相关员工信息");
		}
		YrStaffInfoForm staffInfoForm = ObjectUtils.objectToObject(qr.getResultList().get(0), YrStaffInfoForm.class);
		sqlWhere ="staff_id=?1";
		QueryResult<YrAccount> qrAccount = accountService.find(YrAccount.class, sqlWhere, new Object[]{staffInfoForm.getStaffId()});
		if(qrAccount.getTotalRecord()>0){
			YrAccount account = qrAccount.getResultList().get(0);
			staffInfoForm.setAccountForm(ObjectUtils.objectToObject(account, YrAccountForm.class));
		
			sqlWhere = "account_id=?1";
			QueryResult<YrAccountModelConfig> qrAccountModelConfig = selfService.find(YrAccountModelConfig.class, sqlWhere,new Object[]{staffInfoForm.getAccountForm().getId()});
			List<YrAccountModelConfigForm> accountModelConfigForms = new ArrayList<YrAccountModelConfigForm>();
			for(YrAccountModelConfig entity : qrAccountModelConfig.getResultList()){
				accountModelConfigForms.add(toForm(entity));
			}
			staffInfoForm.setAccountModelConfigForms(accountModelConfigForms);
		}
		List<YrStaffInfoForm> formList = new ArrayList<YrStaffInfoForm>();
		formList.add(staffInfoForm);
		result.setData(formList);
		
		return result;
		
	}
	
	@Override
	public YrAccountModelConfigForm toForm(YrAccountModelConfig e) throws Exception {
		YrAccountModelConfigForm form = ObjectUtils.objectToObject(e, YrAccountModelConfigForm.class);
		return form;
	}

	@Override
	public YrAccountModelConfig toEntity(YrAccountModelConfigForm f) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

}
